import pymysql
import datetime
import random

# 数据库连接参数
host = input("请输入服务器地址:")
port = 2003  # MySQL 默认端口
user = 'root'
password = input("请输入密码:")
db = 'summer_2'

# 创建数据库连接
connection = pymysql.connect(host=host, port=port, user=user, password=password, db=db)

try:
	# 创建 cursor 对象
	with connection.cursor() as cursor:
		# SQL 插入语句
		insert_sql = """
        INSERT INTO day_elec_info (user_id, e_time, e_usage)
        VALUES (%s, %s, %s)
        """
		# 用户列表
		user_id_list = [172119794612536]
		start_date = datetime.date(2024, 5, 1)
		end_date = datetime.date(2024, 7, 19)

		current_date = start_date
		while current_date <= end_date:
			e_time = current_date.strftime("%Y-%m-%d")  # 日期
			for user_id in user_id_list:
				e_usage = random.randint(8, 30)

				# 执行 SQL 语句
				cursor.execute(insert_sql, (user_id, e_time, e_usage))
				# 提交事务
				connection.commit()
			print(current_date.strftime("%Y-%m-%d") + "is over!")
			current_date += datetime.timedelta(days=1)
		print("Data inserted successfully.")

except pymysql.MySQLError as e:
	print(f"Error: {e}")
	connection.rollback()  # 发生错误时回滚
finally:
	# 关闭数据库连接
	connection.close()
